import React, { useEffect } from "react";
import { useLocation, useNavigate } from "react-router-dom";
import { useSelector } from "react-redux";

function RouterBeforeEach(props) {
    var navigate = useNavigate()
    var location = useLocation()
    var token = useSelector((state) => state.userInfo.token)

    //监听路由变化，验证token是否存在
    useEffect(() => {
        if (!token) {
            navigate('/login')
        } else if(location.pathname=='/login'){
            navigate('/index')
        }
    }, [location.pathname])
    return (<></>);
}

export default RouterBeforeEach;



